package name.fowler.refactoring.simplestore.reports;

import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;

import name.fowler.refactoring.simplestore.domain.Customer;
import name.fowler.refactoring.simplestore.domain.Movie;
import name.fowler.refactoring.simplestore.domain.Rental;

public class RentedMoviesReport implements Report {

    private Collection<Customer> customers;

    public RentedMoviesReport(Collection<Customer> customers) {
        this.customers = customers;
    }
    
    public RentedMoviesReport(Customer... customers) {
        this.customers = new LinkedList<Customer>(Arrays.asList(customers));
    }
    
	@Override
	public String generate(Formatter formatter) {
        int count;
        formatter.init();
        formatter.addHeader("Customer movies report");
        
        for(Customer customer: customers) {
            count = 0;
            formatter.addParagraph("");
            formatter.addLine("Customer: " + customer.getName());
            formatter.addBreak();
            for(Rental rental: customer.getRentals()) {
                for(Movie movie: rental.getMovies()) {
                    count++;
                    formatter.addLine("\t" + String.valueOf(count) + ": " + movie.getTitle());
                    formatter.addBreak();
                }
            }
            formatter.addLine("total movies: " + String.valueOf(count));
        }
    	formatter.addFooter("End of report");
    	formatter.finish();
		return formatter.getResult();
	}
}
